package com.kucai.yifaqapp.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerNowActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.NowActivityModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResNow;
import com.kucai.yifaqapp.presenter.activity.NowActivityPresenter;
import com.kucai.yifaqapp.ui.view.NowActivityView;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.PriceUtils;

import java.util.ArrayList;

import static com.kucai.yifaqapp.global.App.getContext;

public class NowActivity extends BaseActivity<NowActivityPresenter> implements NowActivityView {

    LinearLayout titleLeftTv;
    TextView headerTitle;
    SwipeRefreshLayout srl;
    TextView numxiazhu;
    TextView numwin;
    RecyclerView nowlist;
    RelativeLayout skinbg;
    private ArrayList<ResNow.DataBean> nowdata = new ArrayList<>();
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.goback);
        headerTitle = findViewById(R.id.headerTitle);
        srl = findViewById(R.id.srl);
        numxiazhu = findViewById(R.id.numxiazhu);
        numwin = findViewById(R.id.numwin);
        nowlist = findViewById(R.id.nowlist);
        skinbg = findViewById(R.id.skinbg);


        headerTitle.setText("今日已结");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mPresenter.getnow(Info.user.token, "1", "100");
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getnow(Info.user.token, "1", "100");
            }
        });

        nowlist.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter();
        nowlist.setAdapter(adapter);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_now;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    protected void initDagger() {
        DaggerNowActivityComponent.builder()
                .nowActivityModule(new NowActivityModule(this))
                .build().inject(this);
    }

    public String formateRate(String rateStr) {
        if (rateStr.indexOf(".") != -1) {
            //获取小数点的位置
            int num = 0;
            //找到小数点在字符串中的位置,找到返回一个int类型的数字,不存在的话返回 -1
            num = rateStr.indexOf(".");

            String dianAfter = rateStr.substring(0, num + 1);//输入100.30,dianAfter = 100.
            String afterData = rateStr.replace(dianAfter, "");//把原字符(rateStr)串包括小数点和小数点前的字符替换成"",最后得到小数点后的字符(不包括小数点)

            //判断小数点后字符的长度并做不同的操作,得到小数点后两位的字符串
            if (afterData.length() < 2) {
                afterData = afterData + "0";
            } else {
                afterData = afterData;
            }
            //返回元字符串开始到小数点的位置 + "." + 小数点后两位字符
            return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
        } else {
            if (rateStr == "1") {
                return "100";
            } else {
                return rateStr;
            }
        }
    }

    @Override
    public void getdata(ResNow resNow) {
        if (srl != null) {
            srl.setRefreshing(false);
        }
        nowdata.clear();
        nowdata.addAll(resNow.data);
        adapter.notifyDataSetChanged();
        if (null != resNow.otherData) {
            numxiazhu.setText(resNow.totalCount + "");
            if (Double.parseDouble(resNow.otherData.totalResultMoney) > 0) {
                numwin.setText(formateRate(resNow.otherData.totalResultMoney + ""));
                numwin.setTextColor(getContext().getResources().getColor(R.color.orangeRed));
            } else {
                numwin.setText(formateRate(resNow.otherData.totalResultMoney + ""));
                numwin.setTextColor(getContext().getResources().getColor(R.color.Green));
            }
        }
        numxiazhu.setText(resNow.otherData.totalBetMoney + "");
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.nowitem, parent, false));
        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {


            switch (nowdata.get(position).gameId) {
                case 80:
                    //秒速赛车赔率
                    holder.nowname.setText("秒速赛车");
                    break;
                case 50:
                    //北京赛车
                    holder.nowname.setText("北京赛车");
                    break;
                case 82:
                    //秒速快艇
                    holder.nowname.setText("秒速快艇");
                    break;
                case 99:
                    //香港跑马
                    holder.nowname.setText("香港跑马");
                    break;
                case 81:
                    //秒速时时彩
                    holder.nowname.setText("秒速时时彩");
                    break;
                case 1:
                    //重庆时时彩
                    holder.nowname.setText("重庆时时彩");
                    break;
                case 4:
                    //新疆时时彩
                    holder.nowname.setText("新疆时时彩");
                    break;
                case 91:
                    //秒速牛牛
                    holder.nowname.setText("秒速牛牛");
                    break;
                case 90:
                    //pk10牛牛
                    holder.nowname.setText("pk10牛牛");
                    break;
                case 66:
                    //pc蛋蛋
                    holder.nowname.setText("pc蛋蛋");
                    break;
                case 65:
                    //快乐北京8
                    holder.nowname.setText("快乐北京8");
                    break;
                case 86:
                    //秒速快三
                    holder.nowname.setText("秒速快三");
                    break;
                case 10:
                    //江苏快三
                    holder.nowname.setText("江苏快三");
                    break;
                case 12:
                    //广西快三
                    holder.nowname.setText("广西快三");
                    break;
                case 13:
                    //湖北快三
                    holder.nowname.setText("湖北快三");
                    break;
                case 16:
                    //甘肃快三
                    holder.nowname.setText("甘肃快三");
                case 15:
                    //河北快三
                    holder.nowname.setText("河北快三");
                    break;
                case 18:
                    //贵州快三
                    holder.nowname.setText("贵州快三");
                    break;

                case 11:
                    //安徽快三
                    holder.nowname.setText("安徽快三");
                    break;
                case 60:
                    //北京快乐十分
                    holder.nowname.setText("北京快乐十分");
                    break;

                case 61:
                    //幸运农场
                    holder.nowname.setText("幸运农场");
                    break;
                case 21:
                    //十一选五
                    holder.nowname.setText("十一选五");
                    break;
                case 70:
                    //香港六合彩
                    holder.nowname.setText("香港六合彩");
                    break;

                case 85:
                    //幸运六合彩
                    holder.nowname.setText("幸运六合彩");
                    break;
                case 112:
                    //腾讯分分彩
                    holder.nowname.setText("腾讯分分彩");
                    break;
                case 113:
                    //QQ分分彩
                    holder.nowname.setText("QQ分分彩");
                    break;
                case 801:
                    //QQ分分彩
                    holder.nowname.setText("快速赛车");
                    break;
                case 802:
                    //QQ分分彩
                    holder.nowname.setText("快速飞艇");
                    break;
                case 803:
                    //QQ分分彩
                    holder.nowname.setText("快速时时彩");
                    break;
                case 901:
                    //QQ分分彩
                    holder.nowname.setText("三分赛车");
                    break;
                case 902:
                    //QQ分分彩
                    holder.nowname.setText("三分时时彩");
                    break;
                case 903:
                    //QQ分分彩
                    holder.nowname.setText("极速六合彩");
                    break;
                case 904:
                    //QQ分分彩
                    holder.nowname.setText("三分六合彩");
                    break;
                case 55:
                    //QQ分分彩
                    holder.nowname.setText("幸运飞艇");
                    break;

                default:
                    holder.nowname.setText("暂未设置");
                    break;
            }

            holder.nowid.setText(nowdata.get(position).turnNum + "");
            holder.peilv.setText("赔率" + nowdata.get(position).odds + "");
            //已撤单
            if (nowdata.get(position).status == 2) {

                holder.nowwin.setText("已撤单");
                holder.nowwin.setTextColor(getContext().getResources().getColor(R.color.text_color2));
            } else if (nowdata.get(position).status == 1) {
                if (nowdata.get(position).resultMoney == 0) {
                    holder.nowwin.setText("和局");
                    holder.nowwin.setTextColor(getContext().getResources().getColor(R.color.text_color2));
                } else if (nowdata.get(position).resultMoney > 0) {
                    holder.nowwin.setText(nowdata.get(position).resultMoney + "");
                    holder.nowwin.setTextColor(getContext().getResources().getColor(R.color.orangeRed));
                } else if (nowdata.get(position).resultMoney < 0) {
                    holder.nowwin.setText(nowdata.get(position).resultMoney + "");
                    holder.nowwin.setTextColor(getContext().getResources().getColor(R.color.Green));
                }
            }
            holder.nowmoney.setText(nowdata.get(position).money + "");
            Double a = 100 * nowdata.get(position).rebate;
            holder.tuishui.setText("退水" + a);
            holder.mingxi.setText(nowdata.get(position).playCateName + "-" + nowdata.get(position).playName);
            PriceUtils.format(holder.nowwin);
            PriceUtils.format(holder.nowmoney);
        }

        @Override
        public int getItemCount() {
            return nowdata.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView nowname, nowid, tuishui, nowmoney, nowwin, mingxi, peilv;

            MViewHolder(View itemView) {
                super(itemView);
                nowname = (TextView) itemView.findViewById(R.id.nowname);
                nowid = (TextView) itemView.findViewById(R.id.nowid);
                mingxi = (TextView) itemView.findViewById(R.id.mingxi);
                peilv = (TextView) itemView.findViewById(R.id.peilv);
                tuishui = (TextView) itemView.findViewById(R.id.tuishui);
                nowmoney = (TextView) itemView.findViewById(R.id.nowmoney);
                nowwin = (TextView) itemView.findViewById(R.id.nowwin);

            }
        }
    }


}

